/*
 * Copyright [2010] [GP Software]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SqlQueryWrapper.java | Created on April 27, 2010
 */
package org.gp.gpetl.util;

import org.gp.gpetl.config.BeanMapping;

import java.util.List;

/**
 * Wraps an SQL query
 *
 * @author Paul Augustine C. Mendoza
 * @since 1.0
 */
public class SqlQueryWrapper {

    /**
     * The bean mapping
     */
    private BeanMapping beanMapping;

    /**
     * The records
     */
    private List<String[]> records;

    /**
     * The constructor with bean mapping
     *
     * @param beanMapping the bean mapping
     * @param records     the records
     */
    public SqlQueryWrapper(BeanMapping beanMapping, List<String[]> records) {
        this.beanMapping = beanMapping;
        this.records = records;
    }

    /**
     * Gets the table headers
     *
     * @return the headers
     */
    private String[] getHeaders() {
        return MappingUtil.toHeaders(this.beanMapping);
    }

    /**
     * Gets the insert SQL
     *
     * @return the insert SQL
     */
    public String getInsertSql() {
        return SqlUtil
                .getSqlInsertString(getHeaders(), beanMapping.getTablename());
    }

    /**
     * Gets the parameters
     *
     * @param data the data
     * @return the parameters
     */
    public Object[] getParameters(String[] data) {
        return SqlUtil
                .getParameters(getHeaders(), MappingUtil.toMap(beanMapping),
                        data);
    }

    /**
     * Extracts the data and strips off the headers
     *
     * @return the data
     */
    public List<String[]> getData() {
        return records = MappingUtil.extractData(records);
    }
}
